package cn.tedu.bonfire.front.payment.service.impl;

import cn.tedu.bonfire.front.payment.dao.persist.repository.IOrderRepository;
import cn.tedu.bonfire.front.payment.dao.persist.repository.IUserRepository;
import cn.tedu.bonfire.front.payment.pojo.entity.Order;
import cn.tedu.bonfire.front.payment.pojo.entity.User;
import cn.tedu.bonfire.front.payment.pojo.param.OrderParam;
import cn.tedu.bonfire.front.payment.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderRepository orderRepository;

    @Autowired
    IUserRepository userRepository ;

    /**
     * 新建订单
     * @param orderParam
     * @return
     */
    @Override
    public Order createOrder(OrderParam orderParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderParam, order);
        orderRepository.insert(order);
        return order;
    }

    /**
     * 修改订单状态
     * @param outTradeNo
     */
    @Override
    public void updateOrderStatus(String outTradeNo) {
        Order order = new Order();
        order.setOrderNo(outTradeNo);
        Date data = new Date();
        order.setGmtPay(data);
        order.setOrderState(1);
        Order orderByTradeNo = orderRepository.getOrderDetailByOrderTradeNo(outTradeNo);
        order.setId(orderByTradeNo.getId());
        orderRepository.updateOrderStatus(order);
//        User user = new User();
//        user.setId(orderDetailByOrderTradeNo.getBuyerId());
//        user.setBalance( new BigDecimal(outTradeNo));
//        userRepository.updateDalance(user);
//        System.out.println(orderDetailByOrderTradeNo.getTotalPrice());

    }

    /**
     * 检测订单号是否存在
     * @param outTradeNo
     * @return
     */
    @Override
    public Long countByOutTradeNo(String outTradeNo) {
        return orderRepository.countByOutTradeNo(outTradeNo);
    }

    /**
     * 根据订单号获取订单信息
     * @param outTradeNo
     * @return
     */
    @Override
    public Order getByTradeNo(String outTradeNo) {
        return orderRepository.getByTradeNo(outTradeNo);
    }

    /*@Override
    public Double getOrderSum(Long userId) {
        Double orderSum = orderRepository.getOrderSum(userId);
        return orderSum;
    }*/
}
